# auto load
extends Node



const _INTERACT_ACTION := "interact"
var _interaction_areas: Array[InteractionArea]
var _curr_area: InteractionArea = null


func _process(_delta: float) -> void:
    if _interaction_areas.is_empty(): return

    var first := _interaction_areas.front() as InteractionArea
    if first != _curr_area:
        _curr_area = first
        _curr_area.tip_node.show()



func _unhandled_key_input(event: InputEvent) -> void:
    if _curr_area == null: return
    if _curr_area.tip_node.visible and event.is_action_pressed(_INTERACT_ACTION):
        _curr_area.tip_node.hide()
        _curr_area.interacted.emit()
        _interaction_areas.pop_front()
        _curr_area = null



func add_interaction(area: InteractionArea) -> void:
    _interaction_areas.append(area)


func remove_interaction(area: InteractionArea) -> void:
    var index := _interaction_areas.find(area)
    if index == -1: return
    if _curr_area == area:
        _curr_area = null

    area.tip_node.hide()
    _interaction_areas.remove_at(index)


func get_interact_key_text() -> String:
    var text := ""
    for event: InputEvent in InputMap.action_get_events(_INTERACT_ACTION):
        text += event.as_text() + ","
    text.erase(text.length() - 1)
    return text
